package com.yyfax.pay.third.facade.fuiou;

import com.yyfax.framework.common.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Properties;

@Service
public class FuiouProErrorCodeService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private Properties props;

    @PostConstruct
    private void init() throws IOException {
        final String op = "FuiouProErrorCodeService.init";
        logger.info(Log.op(op).msg("开始加载fuiouProErrorCode.properties文件内容.").toString());
        props = new Properties();
        try {
            // 使用spring resource加载文件
            Resource resource = new ClassPathResource("fuiouProErrorCode.properties");
            props = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            logger.error(Log.op(op).msg("加载fuiouProErrorCode.properties文件出现IOException").toString(), e);
            throw e;
        }
        logger.info(Log.op(op).msg("加载fuiouProErrorCode.properties文件内容完成.").toString());
    }

    /**
     * 根据key获取值
     *
     * @author heizq
     * @date 2017-09-20 11:28:33
     * @since v1.0.0
     * @param key
     * @return
     */
    public String getProperty(String key) {
        return props.getProperty(key);
    }

    /**
     * 根据key获取值，没有返回指定的默认值
     *
     * @author heizq
     * @date 2017-09-20 11:28:33
     * @since v1.0.0
     * @param key
     * @return
     */
    public String getProperty(String key, String defaultValue) {
        return props.getProperty(key, defaultValue);
    }
}
